package comm;

import util.Util;
import android.os.Handler;
import android.util.Log;
import entities.Turret;
import entities.TurretPoint;

/** It's the first contact of the MainGamePanel with the framework.
 * It keeps in touch with the Message and ConnectionHandler classes.
 * 
 * @author Bruno Zumba (27/09/2011)
 *
 */
public class CommunicationHandler {
	private String TAG = CommunicationHandler.class.getSimpleName();
	private ConnectionHandler connectionHandler; //Manage the connection with the framework
	
	public CommunicationHandler(Handler outsideHandler) {
		connectionHandler = new ConnectionHandler(outsideHandler);
		if(connectionHandler.isNull()){
			connectionHandler = null;
			return;
		}
	}
	
	public Boolean isNull(){
		if(connectionHandler == null){
			return true;
		}
		
		return false;
	}
	
	public int createTurret(TurretPoint tp, int turretType){
		Message msg = new Message();
		msg.createUpdateMessage(Util.CREATE, tp.getTile().x, tp.getTile().y, turretType);
		
		Log.i(TAG, "Enviando create:\n"+msg.getMsg());
		connectionHandler.send(msg.getMsg());
		
		return 0;
	}
	
	public void sellTurret(Turret turret){
		Message msg = new Message();
		msg.createUpdateMessage(Util.SELL, turret.getTile().x, turret.getTile().y, 0);

		Log.i(TAG, "Enviando sell:\n"+msg.getMsg());
		connectionHandler.send(msg.getMsg());
	}
	
	public void upgradeTurret(Turret turret){
		Message msg = new Message();
		msg.createUpdateMessage(Util.UPGRADE, turret.getTile().x, turret.getTile().y, 0);

		Log.i(TAG, "Enviando upgrade:\n"+msg.getMsg());
		connectionHandler.send(msg.getMsg());
	}
}
